import axios from 'axios'
import Element from 'element-ui'
import router from './router'
import store from './store'


axios.defaults.baseURL = "http://localhost:8081";

// 前置拦截
axios.interceptors.request.use(config => {
    return config;
});

axios.interceptors.response.use(
    response => {
        let res = response.data;

        console.log(res)

        if (res.code === 200) {
            return response;
        } else if (res.code === 401){
            // 登录异常，让他返回登录界面
            Element.Message.error(res.msg);
            store.commit("REMOVE_INFO");
            router.push("/login");
            return Promise.reject(response.data.msg);
        } else {
            Element.Message.error(res.msg);
            return Promise.reject(response.data.msg);
        }

    },
    error => {

        if (error.response.data) {
            error.message = error.response.data.msg;
        }


        if (error.response.status === 401) {
            store.commit("REMOVE_INFO");
            router.push("/login");
        }

        Element.Message.error(error.message);
        return Promise.reject(error);
    }
);